<!--
  File Name: functions.php
  Version 1.0
  O1-31-2011
-->

<?php 

	function makeSQLconnection() {
			$normlink = mysql_connect('localhost', 'root', 'asrai', true);
					if (!$normlink) {
							die('Could not connect: ' . mysql_error());
					} else {
							$hasDB = mysql_select_db('leaveappdb', $normlink);
							if (!$hasDB) {
									echo 
										'Could not choose database:'.mysql_error();
									mysql_close($normlink);
							} else
									$success = true;
					}
					if ($success)        
							return $normlink;
					else
							exit;
	}

	function registerpage(){
		header('Location: register.php');
	}

	function checkEmpNo($number){
		if(strlen($number) != 9)
		{
			return false;
		}
		else
		{
			if(is_numeric($number))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}

	function nameValidator($name) {
		if(strlen($name) == 0)
		{
			return false;
		}
		else
		{
			for($index = 0; $index < strlen($name); $index++)
			{
				if((substr($name, $index, 1) < 'A' || substr($name, $index, 1) > 'Z') && $index == 0)
				{
					return false;
				}
				else if(substr($name, $index, 1) == ' ')
				{
					$index++;
					
					if(substr($name, $index, 1) < 'A' || substr($name, $index, 1) > 'Z')
					{
						return false;
					}
				}
				else if((substr($name, $index, 1) < 'a' || substr($name, $index, 1) > 'z') && $index != 0)
				{
					return false;
				}
			}
		}
		return true;
	}
	
	function transformDate($oldDate){
	
		$year = substr($oldDate, 6);
		$month = substr($oldDate, 0, 2);
		$day = substr($oldDate, 3, 2);
		
		$date = $year . "-" . $month . "-" . $day;
		return $date;
	}
	
	function dateChecker($inputStartDate, $employeeNumber) {
	
		$flag = 0;
		$startYear = substr($inputStartDate, 6);
		$startMonth = substr($inputStartDate, 0, 2);
		$startDay = substr($inputStartDate, 3, 2);
		$data = LeaveManager :: getLeaveData($employeeNumber, "load");
		
		while ($pData = mysql_fetch_assoc($data)) {
			$endDate = $pData['End_date'];
			$flag = 1;
		}
		if ($flag) {
		
			$endYear=substr($endDate, 0, 4);
			$endMonth=substr($endDate, 5, 2);
			$endDay=substr($endDate, 8, 2);
		
		
			if ($startYear <= $endYear) {
				if ($startMonth <= $endMonth) {
					if ($startDay <= $endDay) {
						return false;
					}
				}
			}
			return true;
		}
		
		return false;
	}

	function checkFormat ($date) {
	
		if ( strlen($date) == 0 || strlen($date) != 10 || 
		   strpos($date, "/")!= 2 || substr($date, -5, -4)!="/" ) {
				
				if ( is_numeric(substr($date,0,2)) || 
				     is_numeric(substr($date,3,5)) ||
				     is_numeric(substr($date,6,10)) ) {
					
					return false;
				}
		}
				   
		return true;
	}

	function validateDate ( $month, $day, $year ) {

		$errorFlag = 0;
		$year = intval($year, 10);
		$month = intval($month, 10);
		$day = intval($day, 10);
		
		if ($day>=1 && $month>=1 &&  $month<=12 && $year>=2000) {
							
			//$months with 31 $days
			if ($month==1 || $month==3 || $month == 5 || $month==7 || 
				 $month==8 || $month==10 || $month == 12) {
				
				if ( $day>31 ) {
					$errorFlag = 1;
				}
				
			} else {
				
				if ( $month == 2 ) {
					if ( $year%4 == 0) { 
						if ( $day>29 ) {
							$errorFlag = 1;
						}
					} else {
						if ( $day>28) {
							$errorFlag = 1;
						}
					}							
				} else {
					if ( $day>30 ) {
						$errorFlag = 1;
					}							
				}						
			}
		
		} else {					
			$errorFlag = 1;
		}
			
		if ( $errorFlag==1 ) {
			return false;
		} else {
			return true;
		}
	}
	
?>
